<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">
        <em><img :src="logoUrl" /></em>{{ name }}企业管理系统
      </div>
      <el-form
        :model="param"
        :rules="rules"
        ref="login"
        label-width="0px"
        class="ms-content"
      >
        <el-form-item prop="username">
          <el-input v-model="param.username" placeholder="请输入账号">
            <template #prepend>
              <el-button icon="el-icon-user-solid"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="请输入密码"
            v-model="param.password"
            @keyup.enter="submitForm()"
          >
            <template #prepend>
              <el-button icon="el-icon-lock"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm()">登录</el-button>
          <b
            >还没账号？
            <router-link to="/register">马上注册</router-link>
          </b>
        </div>
        <p class="login-tips">Tips : 用户名和密码随便填。</p>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      logoUrl: '',
      param: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      name: ''
    };
  },
  created() {
    this.$store.commit("clearTags");
    this.login();
  },
  methods: {
    login(){
        this.$http({
          url: "/api/quick/consumer/selectPlatformLogoAndName",
          method: "post",
          headers: {
            "Content-Type": "application/json",
          },
        }).then((data) => {
          if (data.code == 0) {
            localStorage.setItem("platformNo", JSON.stringify(data.result.platformNo));
            if(data.result.logoUrl){
              this.logoUrl = data.result.logoUrl
            }else {
              this.logoUrl = require("../assets/img/photo.jpg")
            }
            this.name = data.result.name
          }else {
            this.logoUrl = require("../assets/img/photo.jpg")
          }
        });
    },
    submitForm() {
      this.$refs.login.validate((valid) => {
        if (valid) {
          this.$http({
            url: "/api/quick/consumer/doLogin",
            method: "post",
            headers: {
              "Content-Type": "application/json",
            },
            data: this.param,
          }).then((data) => {
            if (data.code == 0) {
              this.$store.commit("updateToken", data.result)
              localStorage.setItem("bussines_token", data.result.access_token);
              localStorage.setItem("bussines_info", JSON.stringify(data.result.consumerDto));
              this.$message.success("登录成功");
              if(data.result.checkStatus == 1){
                this.$router.push("/register");
                localStorage.setItem("nextStep", 3);
                localStorage.setItem("nextSteps", 3);
              }else if(data.result.checkStatus == 2) {
                localStorage.setItem("nextStep", 3);
                localStorage.setItem("nextSteps", 2);
                localStorage.setItem("businessPo", JSON.stringify(data.result.businessPo));
                this.$router.push("/register");
              }else if (data.result.checkStatus == 3) {
                localStorage.removeItem("nextStep");
                localStorage.removeItem("nextSteps");
                this.$router.push("/");
              }
            }
          });
        } else {
          this.$message.error("请输入账号和密码");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background: url(../assets/img/login_back.png) center no-repeat;
  background-size: cover;
}
.ms-title {
  width: 100%;
  line-height: 1;
  text-align: center;
  font-size: 18px;
  color: #333;
}
.ms-title em {
  width: 80px;
  height: 80px;
  font-size: 24px;
  color: #4a4848;
  line-height: 80px;
  display: block;
  background: #eef4fb;
  border-radius: 100%;
  font-style: normal;
  margin: 80px auto 20px;
  overflow: hidden;
}
.ms-title em img {
  width: 100%;
  height: 100%;
}
.ms-title b {
  display: block;
  font-weight: normal;
  font-size: 14px;
  margin-top: 5px;
  color: #999;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 610px;
  margin: -355px 0 0 -305px;
  border-radius: 20px;
  overflow: hidden;
  height: 710px;
  background: #fff;
}
.ms-content {
  margin: 100px auto;
  width: 380px;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 220px;
  height: 40px;
  margin-top: 100px;
}
.login-btn b {
  font-size: 14px;
  color: #95a8be;
  display: block;
  text-align: center;
  margin-top: 15px;
  font-weight: normal;
}
.login-btn a {
  color: #326cf0;
}
.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #fff;
  display: none;
}
</style>
<style>
.login-wrap .el-input__inner {
  border: 0;
  line-height: 40px;
  height: 40px;
  color: #b9c0c7;
  background: #fff;
}
.login-wrap .el-input-group__prepend {
  color: #b9c0c7;
  padding: 0 10px 0 0;
  background: none;
  border: 0;
  font-size: 20px;
}
.login-wrap .el-form-item {
  margin-bottom: 0;
  border-bottom: 1px solid #f8fafc;
  line-height: 40px;
  width: 380px;
  margin-top: 30px;
}
</style>